package com.abel.bigwater.data.jnrq

import com.abel.bigwater.data.BcdString
import org.junit.Test
import org.slf4j.LoggerFactory
import java.net.InetSocketAddress
import java.nio.ByteBuffer
import java.nio.channels.AsynchronousSocketChannel
import java.util.concurrent.TimeUnit

class JnrqClientTests {
    @Test
    fun testDownload() {
        // aj-8278331050004239
        val str = """68 11 55 46 96 eb 00 00 31 00 01 00 00 0c 00 00 8b 8b 46 08 57 09 39 42 00 50 10 33 78 82 2b 28
0d 19 07 e4 07 00 8c 37 05 00 00 00 00 00 d2 0d 0a """
        // aj-8278332016053005
        val str2 = """
68 11 55 46 f9 cf 00 00 55 00 03 00 00 1f 00 00 3f 3f 6b 4a bb 05 05 30 05 16 20 33 78 82 1a 25
0f 0b 04 e3 07 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 2a 0d 0a """

        val sk = AsynchronousSocketChannel.open()
        val f1 = sk.connect(LOCAL_SERVER)
        f1[1, TimeUnit.SECONDS]

        val buf = ByteBuffer.wrap(BcdString.toByteArray(str))
        val f2 = sk.write(buf)
        f2[2, TimeUnit.SECONDS]

        val dst = ByteBuffer.allocate(2048)
        val f3 = sk.read(dst)
        f3[2, TimeUnit.SECONDS]
        dst.flip()
        val ba = ByteArray(dst.remaining())
        dst.get(ba)
        lgr.info("Got from jnrq: {}", BcdString.hexString(ba))

        sk.shutdownInput()
        sk.shutdownOutput()
        sk.close()
    }

    /**
     * 68 11 55 45 f9 cf 00 00 19 00 00 01 01 01 01 00 00 00 00 00 bb 00 b3 0d 0a
     */
    @Test
    fun testDownloadAnswer() {
        val str = """68 11 55 45 f9 cf 00 00 19 00 00 01 01 01 01 00 00 00 00 00 bb 00 b3 0d 0a"""

        val sk = AsynchronousSocketChannel.open()
        val f1 = sk.connect(LOCAL_SERVER)
        f1[1, TimeUnit.SECONDS]

        val buf = ByteBuffer.wrap(BcdString.toByteArray(str))
        val f2 = sk.write(buf)
        f2[2, TimeUnit.SECONDS]

        val dst = ByteBuffer.allocate(2048)
        val f3 = sk.read(dst)
        f3[2, TimeUnit.SECONDS]
        dst.flip()
        val ba = ByteArray(dst.remaining())
        dst.get(ba)
        lgr.info("Got from jnrq: {}", BcdString.hexString(ba))

        sk.shutdownInput()
        sk.shutdownOutput()
        sk.close()

    }

    /**
     * 68 11 55 45 f9 cf 00 00 19 00 00 01 01 01 01 00 00 00 00 00 bb 00 b3 0d 0a
     */
    @Test
    fun testDownloadIpPort() {
        val str = """68 11 55 45 f9 cf 00 00 19 00 00 01 01 01 01 00 00 00 00 00 bb 00 b3 0d 0a"""

        val sk = AsynchronousSocketChannel.open()
        val f1 = sk.connect(LOCAL_SERVER)
        f1[1, TimeUnit.SECONDS]

        val buf = ByteBuffer.wrap(BcdString.toByteArray(str))
        val f2 = sk.write(buf)
        f2[2, TimeUnit.SECONDS]

        val dst = ByteBuffer.allocate(2048)
        val f3 = sk.read(dst)
        f3[2, TimeUnit.SECONDS]
        dst.flip()
        val ba = ByteArray(dst.remaining())
        dst.get(ba)
        lgr.info("Got from jnrq: {}", BcdString.hexString(ba))

        sk.shutdownInput()
        sk.shutdownOutput()
        sk.close()

    }

    companion object {
        private val lgr = LoggerFactory.getLogger(JnrqClientTests::class.java)

        private val LOCAL_SERVER = InetSocketAddress("localhost", 9516)
    }

}